package top.infopub.mgr.adm.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.adm.dao.MeetingRecordAttDao;
import top.infopub.mgr.adm.dao.MeetingRecordDao;
import top.infopub.mgr.adm.domain.MeetingRecordDomain;
import top.infopub.model.adm.MeetingRecord;
import top.infopub.model.adm.MeetingRecordAtt;
import top.infopub.mgr.adm.service.MeetingRecordService;
import top.infopub.mgr.adm.vo.MeetingRecordVo;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;

@Service
public class MeetingRecordServiceImpl extends GenericServiceImpl<MeetingRecord, String> implements
		MeetingRecordService {

	@Autowired
	private MeetingRecordDao meetingRecordDao;
	
	@Autowired
	private MeetingRecordAttDao meetingRecordAttDao;
	
	@Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<MeetingRecord, String> getDao() {
		return meetingRecordDao;
	}

    @Override
    public Page<MeetingRecordDomain> selectByDomainWhere(Page<MeetingRecordDomain> page,
                                                         MeetingRecordVo meetingRecordVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<MeetingRecordDomain>)meetingRecordDao.selectByDomainWhere(meetingRecordVo);
    }

    @Override
    public Page<MeetingRecordDomain> selectMyCheckByDomainWhere(Page<MeetingRecordDomain> page,
                                                                MeetingRecordVo meetingRecordVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<MeetingRecordDomain>)meetingRecordDao.selectMyCheckByDomainWhere(meetingRecordVo);
    }

    @Override
    public List<MeetingRecord> isExistTime(MeetingRecord meetingRecord) {
        return meetingRecordDao.isExistTime(meetingRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMeetingRecord(MeetingRecord meetingRecord,
                                  List<MeetingRecordAtt> meetingRecordAtts) {
        meetingRecordDao.insertById(meetingRecord);
        
        if(CollectionUtils.isNotEmpty(meetingRecordAtts)){
            meetingRecordAttDao.insertByBatch(meetingRecordAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.ADM_MEETING_RECORD;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, meetingRecord.getId(), meetingRecord.getProcessId(), null);
        
    }

    @Override
    public MeetingRecordVo queryById(String id) {
        return meetingRecordDao.queryById(id);
    }
}